<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" class="tl-dialog" width="1000px" @open="open" @close="close" top="10px" :append-to-body="true" :modal-append-to-body="true">
    <div style="padding-bottom:10px">
      <!-- 查询界面 -->
      <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false">
        <template slot="search">
          <el-form-item label="里程碑模板名称">
            <el-input v-model="queryParam.qry_milepost" placeholder="模板名称"></el-input>
          </el-form-item>
        </template>
      </tl-list-search>
      <!-- 列表界面 -->
      <div>
        <!-- 列表选择 -->
        <div class="table-alert-div" ref="selector">
          <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
            <template slot="title">
              已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRows.length }}</a>项&nbsp;&nbsp;
              <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
            </template>
          </el-alert>
        </div>
        <!--表格渲染-->
        <el-table class="tl-table" ref="mainTable" :height="tableHeight" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" row-key="id" border fit :row-class-name="doChangeRowClassName">
          <el-table-column type="selection" width="55px" fixed="left" v-if="isSingleSelect === false "></el-table-column>
          <el-table-column prop="name" label="里程碑模板名称" width="240px" sortable="custom" sort-by="name" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="deliverable" label="可交付成果" sortable="custom" sort-by="deliverable" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="操作" width="80px" align="center" fixed="right" v-if="isSingleSelect === true">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-finished" @click="doSelectData(scope.row[fieldDefaultKey])">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 列表分页 -->
        <el-row class="pagebar" ref="pager">
          <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
        </el-row>
      </div>
    </div>
    <div slot="footer" class="dialog-footer" v-if="isSingleSelect === false">
      <el-button type="text" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doConfirm">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_ref_list } from '@tapui/mixins/mixin_ref_list'
/** 子组件 */
/** 工具类 */
import { queryDataCode } from '@tapui/api/dataCode'
import { validateNull } from '@tapui/utils/validate'
import { filterObjectNull } from '@tapui/utils/util'
import { getAction, putAction, postAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  components: {},
  mixins: [mixin_ref_list],
  data() {
    return {
      description: '按模板新增',
      // 默认Key字段名
      fieldDefaultKey: 'id',
      // 默认显示字段名
      fieldDefaultValue: 'name',
      // url配置
      url: {
        list: '/sd/milePostTemplate/refList',
        insert: '/sd/projectMilepost/insertFromMilePostTemplate'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'sortcode',
        sord: 'asc'
      },
      addFromPlan: false
    }
  },
  props: {
    parentId: {
      type: [Number, String],
      required: true
    },
    projectinfo: {
    }
  },
  methods: {
    getQueryParams() {
      let param = {}
      Object.assign(param, this.queryParam, this.externalQueryParam, this.filtersInfo, this.sorterInfo)
      param.currentPage = this.pageInfo.currentPage
      param.pageSize = this.pageInfo.pageSize
      param.qry_projectlevel = this.projectinfo.projectlevel
      return filterObjectNull(param)
    },
    doConfirm() {
      let selectedValues = ''
      let selectedKeys = ''
      let params = {}
      this.selectedRows.forEach(item => {
        selectedKeys += item[this.fieldKeyComputed] + ','
      })
      if (selectedKeys.length > 0) {
        selectedKeys = selectedKeys.substring(0, selectedKeys.length - 1)
      } else {
        this.$tap.alert('请勾选需要插入的里程碑！', 'warning')
        return
      }
      this.$set(params, 'keys', selectedKeys)
      this.$set(params, 'planid', this.parentId)
      this.$set(params, 'projectcode', this.projectinfo.projectcode)
      postAction(this.url.insert, params).then(res => {
        if (res.success) {
          this.$tap.message(res.message, 'success')
          if (this.addFromPlan) {
            this.$parent.$parent.milepostLoadInfo()
          }
          this.$emit('refresh')
        } else {
          this.$tap.alert(res.message, 'warning')
        }
        this.saveLoading = false
      })
      this.visible = false
    }
  }
}
</script>

